package xjay.ai.emotionalsupport.controller;

import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xjay.ai.emotionalsupport.agent.CustomizeAgent;
import xjay.ai.emotionalsupport.agent.DivinationAgent;
import xjay.ai.emotionalsupport.agent.MaidAgent;
import xjay.ai.emotionalsupport.agent.TarotAgent;
import xjay.ai.emotionalsupport.dto.ChatRequest;
import xjay.ai.emotionalsupport.dto.CustomizedRequest;

import java.io.IOException;

@Slf4j
@RestController
public class AgentController {//单例

    @Resource
    private DivinationAgent divinationAgent;

    @Resource
    private TarotAgent tarotAgent;

    @Resource
    private MaidAgent maidAgent;

    private Boolean divinationColdStart = true;
    private Boolean tarotColdStart = true;
    private Boolean maidColdStart = true;
    private Boolean customizeColdStart = true;
    @Autowired
    private CustomizeAgent customizeAgent;

    @PostMapping("/agent/divination_agent")
    public String doChatWithDivinationAgent(@RequestBody ChatRequest request) {
        String userMessage = request.getUserMessage();
        String userId = request.getUserId();
        String sessionId = request.getSessionId();
        // 冷启动处理
        if (divinationColdStart) {

            synchronized (this) {
                if (divinationColdStart) {
                    // 系统级初始化
                    divinationAgent.warmUp(userId, sessionId);
                    divinationColdStart = false;
                }
            }
        }
        return divinationAgent.divinationChat(
            userMessage, userId, sessionId
        );
    }

    @PostMapping("/agent/tarot_agent")
    public String doChatWithTarotAgent(@RequestBody ChatRequest request) {
        String userMessage = request.getUserMessage();
        String userId = request.getUserId();
        String sessionId = request.getSessionId();
        // 冷启动处理

        if (tarotColdStart) {
            synchronized (this) {
                if (tarotColdStart) {
                    // 系统级初始化
                    tarotAgent.warmUp(userId, sessionId);
                    tarotColdStart = false;
                }
            }
        }
        return tarotAgent.tarotChat(
                userMessage, userId, sessionId
        );
    }


    @PostMapping("/agent/maid_agent")
    public String doChatWithMaidAgent(@RequestBody ChatRequest request) {
        String userMessage = request.getUserMessage();
        String userId = request.getUserId();
        String sessionId = request.getSessionId();
        // 冷启动处理

        if (maidColdStart) {
            synchronized (this) {
                if (maidColdStart) {
                    // 系统级初始化
                    maidAgent.warmUp(userId, sessionId);
                    maidColdStart = false;
                }
            }
        }
        return maidAgent.maidChat(
                userMessage, userId, sessionId
        );
    }

    @PostMapping("/agent/customize_agent")
    public String doChatWithCustomizeAgent(@RequestBody CustomizedRequest request) throws IOException {

        String userMessage = request.getUserMessage();
        String userId = request.getUserId();
        String sessionId = request.getSessionId();

        String toStart = request.getToStart(); //开场白
        String characterDefinition = request.getCharacterDefinition();
        String userProfile = request.getUserProfile();

        StringBuilder stringBuilder = new StringBuilder();
        if(customizeColdStart) {
            synchronized (this) {
                if (customizeColdStart) {
                    customizeAgent.customizeInit(characterDefinition, userProfile);
                    stringBuilder.append(toStart).append("\n");
                    customizeAgent.warmUp(userId, sessionId);
                    customizeColdStart = false;
                }
            }
        }
        stringBuilder.append(customizeAgent.customizeChat(userMessage,userId,sessionId));
        return stringBuilder.toString();
    }

}
